<template>
  <div class="box">
    <header class="header kinds">
      <van-nav-bar left-arrow @click-left="$router.push('/home')">
        <template #title>
          <van-search
            v-model="value"
            shape="round"
            placeholder="请输入搜索关键词"
            @click="$router.push('/search')"
          />
        </template>
      </van-nav-bar>
    </header>
    <div class="content kind">
      <van-sidebar v-model="activeKey" @change="onChange">
        <van-sidebar-item v-for="item in columnList" :key="item" :title="item" />
      </van-sidebar>
      <van-grid>
        <van-grid-item :icon="item.logo" v-for="item in brandList" :key="item.brand" :text="item.brand" @click="searchBrand(item.brand)" />
      </van-grid>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Sidebar, SidebarItem, Grid, GridItem, NavBar, Search } from 'vant'
import { getColumn, getColumnBrand } from '../../api/index.js'

Vue.use(Sidebar)
Vue.use(SidebarItem)
Vue.use(Grid)
Vue.use(GridItem)
Vue.use(NavBar)
Vue.use(Search)

export default {
  data () {
    return {
      activeKey: 0,
      columnList: [],
      brandList: [],
      value: ''
    }
  },
  mounted () {
    getColumn().then(res => {
      // console.log(res.data.data)
      this.columnList = res.data.data
      this.onChange(0)
    })
  },
  methods: {
    onChange (idx) {
      console.log(idx)
      this.activeKey = idx
      getColumnBrand({
        column: this.columnList[idx]
      }).then(res => {
        console.log(res.data.data)
        this.brandList = res.data.data
      })
    },
    searchBrand (a) {
      const b = this.columnList[this.activeKey]
      this.$router.push('/kindSearch?column=' + b + '&brand=' + a)
    }
  }
}
</script>

<style lang="scss">
  .kinds {
    .van-nav-bar__title {
      width: 80%;
      max-width: 80%;
      .van-search {
        height: .38rem;
        line-height: .38rem;
      }
    }
  }
  .kind.content {
    margin: 0;
    padding: 0;
    display: flex;
    .van-sidebar {
      width: 1rem;
      .van-sidebar-item {
        &::before {
          background: #FFFFFF;
        }
        &.van-sidebar-item--select {
          .van-sidebar-item__text {
            color: red;
          }
        }
      }
    }
    .van-grid {
      flex: 1;
      align-self: flex-start;
    }
  }
</style>
